//
//  DWViewController.m
//  Item A House
//
//  Created by 甄奇文 on 15/5/29.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DWViewController.h"
#import <CoreLocation/CoreLocation.h>
#import "CUSFlashLabel.h"

@interface DWViewController ()<CLLocationManagerDelegate>
@property (nonatomic, retain) CUSFlashLabel *LatitudeLabel; // 经度
@property (nonatomic, retain) CUSFlashLabel *LongitudeLabel;  // 纬度
@property (nonatomic, retain) CUSFlashLabel *addressLabel;

// 定义一个全局的变量用来记录CLLocationManager对象
@property(nonatomic, strong) CLLocationManager *locationManager;
@end

@implementation DWViewController
- (void)dealloc
{
    [_LatitudeLabel release];
    [_LongitudeLabel release];
    [_addressLabel release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:0.1 green:0 blue:0 alpha:0.2];
    self.navigationController.navigationBar.translucent = NO;
    
    


    self.LatitudeLabel = [[CUSFlashLabel alloc] initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 50)];
   
    self.LatitudeLabel.textAlignment = NSTextAlignmentCenter;
    [self.LatitudeLabel setFont:[UIFont systemFontOfSize:25]];
    [self.LatitudeLabel setSpotlightColor:[UIColor blueColor]];
    [self.LatitudeLabel startAnimating];
    [self.view addSubview:self.LatitudeLabel];
    [self.LatitudeLabel release];
    
    
    self.LongitudeLabel = [[CUSFlashLabel alloc] initWithFrame:CGRectMake(0, 110, [UIScreen mainScreen].bounds.size.width, 50)];
    [self.LongitudeLabel setFont:[UIFont systemFontOfSize:25]];
    [self.LongitudeLabel setSpotlightColor:[UIColor blueColor]];
    self.LongitudeLabel.textAlignment = NSTextAlignmentCenter;
    [self.LongitudeLabel startAnimating];
    [self.view addSubview:self.LongitudeLabel];
    [self.LongitudeLabel release];

    
    self.addressLabel = [[CUSFlashLabel alloc] initWithFrame:CGRectMake(0, 180, [UIScreen mainScreen].bounds.size.width, 80)];
    [self.addressLabel setFont:[UIFont systemFontOfSize:20
                                ]];
    [self.addressLabel setTextColor:[UIColor redColor]];
    [self.addressLabel setSpotlightColor:[UIColor yellowColor]];
    self.addressLabel.textAlignment = NSTextAlignmentLeft;
    [self.addressLabel setContentMode:UIViewContentModeBottom];
    self.addressLabel.numberOfLines = 0;
    [self.addressLabel startAnimating];
    [self.view addSubview:self.addressLabel];
    [self.addressLabel release];
    
    
    
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.distanceFilter = 100; // 位置发生100米偏移时  重新定位
    
    
    
    
    // 始终允许访问位置信息
    [self.locationManager requestAlwaysAuthorization];
    
    [self.locationManager startUpdatingLocation];
     //使用应用程序期间允许访问位置数据
}

#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    
    CLLocation *currLocation = [locations lastObject];
    self.LatitudeLabel.text = [NSString stringWithFormat:@"经度：%f", currLocation.coordinate.latitude];
    self.LongitudeLabel.text = [NSString stringWithFormat:@"纬度：%f", currLocation.coordinate.longitude];
    
    NSLog(@"经度＝%f 纬度＝%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude);
    // 关闭定位
    [self.locationManager stopUpdatingLocation];
    
    
    // 根据经纬度 反向编码  获取详细地址
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:currLocation
                   completionHandler:^(NSArray *placemarks, NSError *error){
                       
                       for (CLPlacemark *place in placemarks) {
                           
                           self.addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@ %@ %@", place.country, place.subLocality, place.locality, place.subThoroughfare, place.thoroughfare];
                           
                           NSLog(@"thoroughfare,%@",place.thoroughfare);       // 街道
                           NSLog(@"subThoroughfare,%@",place.subThoroughfare); // 子街道
                           NSLog(@"locality,%@",place.locality);         // 市
                           NSLog(@"subLocality,%@",place.subLocality);         // 区
                           NSLog(@"country,%@",place.country);                 // 国家
                       }
                       
                   }];
    
}





@end
